﻿Imports Kimmo

Public Class frmColumns
    Inherits System.Web.UI.Page
    Private strID As String = ""
    Private strAction As String = ""
    Private okScript As String = ""
    Private strView As String = ""
    Private strProjectID As String = ""
    Private strProjectName As String = ""
    Private WithEvents btRefresh As FineUI.Button
    Private WithEvents btNew As FineUI.Button
    Private strTableID As String = ""
    Private strTableName As String = ""
    Private WithEvents btPositions As FineUI.Button
    Private WithEvents btBE As FineUI.Button
    Private WithEvents btUI As FineUI.Button
    Private WithEvents btDE As FineUI.Button

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.btNew = clsFineUI.ToolBar.AddClickButton(tblLeft, "New", "~/images/newsomething16.gif", "Add a new column")
        Me.btPositions = clsFineUI.ToolBar.AddClickButton(tblLeft, "Positions", "~/images/positions.gif", "Ensure positions are in sequence starting from 01")
        Me.btBE = clsFineUI.ToolBar.AddClickButton(tblLeft, "Business Object", "~/images/be.gif", "Generate the business entity object for this table")
        Me.btUI = clsFineUI.ToolBar.AddClickButton(tblLeft, "User Interface", "~/images/ui.gif", "Generate the user interface details screen for this table")
        Me.btDE = clsFineUI.ToolBar.AddClickButton(tblLeft, "Designer", "~/images/design.gif", "Generate the user interface designer screen for this table")

        Me.btRefresh = clsFineUI.ToolBar.AddClickButton(tblLeft, "Refresh", "~/images/refresh.gif", "Refresh columns for table")

        strID = WebSite.GetQueryValue("id")
        strAction = WebSite.GetQueryValue("action")
        strView = WebSite.GetQueryValue("view")
        strProjectID = WebSite.GetQueryValue("projectid")
        strTableID = WebSite.GetQueryValue("tableid")
        If Len(strTableID) > 0 Then clsDL.SaveSetting("tableid", strTableID)
        If Len(strProjectID) > 0 Then clsDL.SaveSetting("projectid", strProjectID)
        If Len(strID) > 0 Then clsDL.SaveSetting("id", strID)
        If Len(strAction) > 0 Then clsDL.SaveSetting("action", strAction)
        If Len(strView) > 0 Then clsDL.SaveSetting("view", strView)
        If IsPostBack Then
        Else
            Select Case strAction
                Case "delete"
                    Columns_DeleteRecords()
            End Select
        End If
        Columns_Report()
    End Sub

    Protected Sub Columns_DeletePrompt(ByVal StrID As String)
        clsDL.SaveSetting("deleteid", StrID)
        Me.okScript = "window.location.href='frmColumns.aspx?action=delete';"
        clsFineUI.MyConfirmation("Confirm Delete", "Are you sure that you want to delete this column definition? If you choose Yes, your actions cannot be undone. Delete?", Me.okScript, "")
    End Sub

    Protected Sub Columns_Save(ByRef row As FineUI.GridRow, ByVal strDK As String)
        Dim clsC As New clsColumns
        clsC.Read(strDK)
        clsC.Position = clsDL.TextBoxValue(row, "Position")
        clsC.Position = clsDL.StrFormat(Val(clsC.Position), "R02")
        clsC.ColumnName = clsDL.TextBoxValue(row, "ColumnName")
        clsC.ColumnName = Files.File_CleanName(clsC.ColumnName)
        clsC.DataType = clsDL.ComboBoxValue(row, "DataType")
        clsC.Caption = clsDL.TextBoxValue(row, "Caption")
        clsC.EmptyText = clsDL.TextBoxValue(row, "EmptyText")
        clsC.ToolTip = clsDL.TextBoxValue(row, "ToolTip")
        clsC.DataLength = Val(clsDL.TextBoxValue(row, "DataLength"))
        clsC.DefaultValue = clsDL.TextBoxValue(row, "DefaultValue")
        clsC.ControlType = clsDL.ComboBoxValue(row, "ControlType")
        clsC.DataFormat = clsDL.ComboBoxValue(row, "DataFormat")
        clsC.ColumnWidth = Val(clsDL.TextBoxValue(row, "ColumnWidth"))
        clsC.TextAlign = clsDL.ComboBoxValue(row, "TextAlign")
        clsC.IsVisible = clsDL.CheckBoxValueYN(row, "IsVisible")
        clsC.IsSortable = clsDL.CheckBoxValueYN(row, "IsSortable")
        clsC.IsPrimary = clsDL.CheckBoxValueYN(row, "IsPrimary")
        clsC.IsNull = clsDL.CheckBoxValueYN(row, "IsNull")
        clsC.IsAuto = clsDL.CheckBoxValueYN(row, "IsAuto")
        clsC.IsOnGrid = clsDL.CheckBoxValueYN(row, "IsOnGrid")
        clsC.IsIndex = clsDL.CheckBoxValueYN(row, "IsIndex")
        clsC.IsRequired = clsDL.CheckBoxValueYN(row, "IsRequired")
        clsC.IsSearchable = clsDL.CheckBoxValueYN(row, "IsSearchable")
        clsC.IsGridToolTip = clsDL.CheckBoxValueYN(row, "IsGridToolTip")
        clsC.IsGroup = clsDL.CheckBoxValueYN(row, "IsGroup")
        clsC.IsSum = clsDL.CheckBoxValueYN(row, "IsSum")
        clsC.IsReadOnly = clsDL.CheckBoxValueYN(row, "IsReadOnly")
        clsC.IsExpand = clsDL.CheckBoxValueYN(row, "IsExpand")
        clsC.IsAutoPostBack = clsDL.CheckBoxValueYN(row, "IsAutoPostBack")
        clsC.Save()
        clsDL.SaveSetting("view", "columns")
        clsDL.SaveSetting("tableid", clsC.TableID)
        Columns_Report()
    End Sub

    Protected Sub Columns_DeleteRecords()
        strID = clsDL.ReadSetting("deleteid")
        If Len(strID) > 0 Then
            strTableID = clsColumns.TableIDByID(strID)
            clsColumns.DeleteByID(strID)
            clsDL.SaveSetting("tableid", strTableID)
            clsDL.SaveSetting("view", "columns")
        End If
        WebSite.GotoPage("frmColumns.aspx")
    End Sub

    Protected Sub Columns_Report()
        Me.regLeft.Title = "Columns"
        strID = clsDL.ReadSetting("id")
        strView = clsDL.ReadSetting("view")
        strProjectID = clsDL.ReadSetting("projectid")
        strTableID = clsDL.ReadSetting("tableid")
        Select Case strView
            Case "columns"
                If Len(strTableID) > 0 Then
                    strProjectName = clsTables.ProjectNameByID(strTableID)
                    strTableName = clsTables.TableNameByID(strTableID)
                    regLeft.Title = "Columns: " & strProjectName & "\" & strTableName
                    clsColumns.ShowColumns(grdViews, "TableID = " & Val(strTableID))
                Else
                    clsColumns.ShowColumns(grdViews, "")
                End If
            Case Else
                clsColumns.ShowColumns(grdViews, "")
        End Select
    End Sub

    Private Sub Columns_Clone(ByVal StrID As String)
        Dim nSV As New clsColumns()
        nSV.Read(StrID)
        nSV.ID = CLng(-1)
        nSV.ColumnName = "Copy of " & nSV.ColumnName
        nSV.Save()
        nSV.Position = clsDL.StrFormat(nSV.ID, "R02")
        nSV.Save()
        clsDL.SaveSetting("view", "columns")
        clsDL.SaveSetting("tableid", nSV.TableID)
        Columns_Report()
    End Sub

    Public Sub grdViews_PageIndexChange(sender As Object, e As FineUI.GridPageEventArgs) Handles grdViews.PageIndexChange
        Me.grdViews.PageIndex = e.NewPageIndex
    End Sub

    Public Sub grdViews_RowCommand(sender As Object, e As FineUI.GridCommandEventArgs) Handles grdViews.RowCommand
        Dim strCommand As String = e.CommandName
        Dim strDK As String = clsFineUI.Grid.RowID(grdViews, e.RowIndex, 0)
        Dim row As FineUI.GridRow = Me.grdViews.Rows(e.RowIndex)
        Select Case strCommand
            Case "save" : Me.Columns_Save(row, strDK)
            Case "clone" : Me.Columns_Clone(strDK)
            Case "delete" : Me.Columns_DeletePrompt(strDK)
            Case "wizard" : Wizard(strDK)
        End Select
    End Sub

    Private Sub Wizard(ByVal strColumnID As String)
        Dim clsC As New clsColumns
        clsC.Read(strColumnID)
        clsC.ColumnName = clsC.ColumnName.Replace(" ", "").Trim
        clsC.ColumnName = Files.File_CleanName(clsC.ColumnName)
        clsC.Caption = clsDL.FormatColumn(clsC.ColumnName)
        clsC.EmptyText = "Enter " & clsC.Caption.ToLower & " here."
        clsC.ToolTip = clsC.Caption
        clsC.Save()
        Columns_Report()
    End Sub

    Private Sub btRefresh_Click(sender As Object, e As EventArgs) Handles btRefresh.Click
        clsDL.SaveSetting("view", "columns")
        Columns_Report()
    End Sub

    Private Sub btNew_Click(sender As Object, e As EventArgs) Handles btNew.Click
        Dim strTableID As String = clsDL.ReadSetting("tableid")
        If Len(strTableID) > 0 Then
            clsColumns.Columns_Blank(strTableID)
            Columns_Report()
        Else
            clsFineUI.MyAlert("Table Error", "Please select the table to add a column to first!")
        End If
    End Sub

    Private Sub btPositions_Click(sender As Object, e As EventArgs) Handles btPositions.Click
        Dim strTableID As String = clsDL.ReadSetting("tableid")
        If Len(strTableID) > 0 Then
            clsColumns.FixPositions(strTableID)
            Columns_Report()
        Else
            clsFineUI.MyAlert("Table Error", "Please select the table to cascade positions for!")
        End If
    End Sub

    Private Sub btBE_Click(sender As Object, e As EventArgs) Handles btBE.Click
        Dim strTableID As String = clsDL.ReadSetting("tableid")
        If Len(strTableID) > 0 Then
            Dim strBE As String = clsBusinessEntity.Build(strTableID)
            ' add the file to the sourcefiles table
            ' read table properties
            Dim clsT As New clsTables
            clsT.Read(strTableID)
            ' is the file listed in the database
            Dim strID As String = clsSourceFiles.IDByFilePath(strBE).ToString
            ' record does not exist, add it
            If Val(strID) = 0 Then
                Dim clsSF As New clsSourceFiles
                clsSF.IsNew = True
                clsSF.ID = -1
                clsSF.TableId = Val(strTableID)
                clsSF.TableName = clsT.TableName
                clsSF.ProjectID = clsT.ProjectID
                clsSF.ProjectName = clsT.ProjectName
                clsSF.FilePath = strBE
                clsSF.ClassName = clsDL.MvField(clsSF.FilePath, -1, "/")
                clsSF.Save()
            Else
                Dim clsSF As New clsSourceFiles
                clsSF.Read(strID)
                clsSF.TableId = Val(strTableID)
                clsSF.TableName = clsT.TableName
                clsSF.ProjectID = clsT.ProjectID
                clsSF.ProjectName = clsT.ProjectName
                clsSF.FilePath = strBE
                clsSF.ClassName = clsDL.MvField(clsSF.FilePath, -1, "/")
                clsSF.Save()
            End If
            ' open source code file
            WebSite.GotoPage("frmSourceCode.aspx?file=" & strBE)
        Else
            clsFineUI.MyAlert("Table Error", "Please select the table to build the business object for!")
        End If
    End Sub

    Private Sub btUI_Click(sender As Object, e As EventArgs) Handles btUI.Click
        Dim strTableID As String = clsDL.ReadSetting("tableid")
        If Len(strTableID) > 0 Then
            Dim strUI As String = clsFormDetails.Build(strTableID)
            ' add the file to the sourcefiles table
            ' read table properties
            Dim clsT As New clsTables
            clsT.Read(strTableID)
            ' is the file listed in the database
            Dim strID As String = clsSourceFiles.IDByFilePath(strUI).ToString
            ' record does not exist, add it
            If Val(strID) = 0 Then
                Dim clsSF As New clsSourceFiles
                clsSF.IsNew = True
                clsSF.ID = -1
                clsSF.TableId = Val(strTableID)
                clsSF.TableName = clsT.TableName
                clsSF.ProjectID = clsT.ProjectID
                clsSF.ProjectName = clsT.ProjectName
                clsSF.FilePath = strUI
                clsSF.ClassName = clsDL.MvField(clsSF.FilePath, -1, "/")
                clsSF.Save()
            Else
                Dim clsSF As New clsSourceFiles
                clsSF.Read(strID)
                clsSF.TableId = Val(strTableID)
                clsSF.TableName = clsT.TableName
                clsSF.ProjectID = clsT.ProjectID
                clsSF.ProjectName = clsT.ProjectName
                clsSF.FilePath = strUI
                clsSF.ClassName = clsDL.MvField(clsSF.FilePath, -1, "/")
                clsSF.Save()
            End If
            ' open source code file
            WebSite.GotoPage("frmSourceCode.aspx?file=" & strUI)
        Else
            clsFineUI.MyAlert("Table Error", "Please select the table to build the business object for!")
        End If

    End Sub

    Private Sub btDE_Click(sender As Object, e As EventArgs) Handles btDE.Click
        Dim strTableID As String = clsDL.ReadSetting("tableid")
        If Len(strTableID) > 0 Then
            Dim strDE As String = clsFormDesigner.Build(strTableID)
            ' add the file to the sourcefiles table
            ' read table properties
            Dim clsT As New clsTables
            clsT.Read(strTableID)
            ' is the file listed in the database
            Dim strID As String = clsSourceFiles.IDByFilePath(strDE).ToString
            ' record does not exist, add it
            If Val(strID) = 0 Then
                Dim clsSF As New clsSourceFiles
                clsSF.IsNew = True
                clsSF.ID = -1
                clsSF.TableId = Val(strTableID)
                clsSF.TableName = clsT.TableName
                clsSF.ProjectID = clsT.ProjectID
                clsSF.ProjectName = clsT.ProjectName
                clsSF.FilePath = strDE
                clsSF.ClassName = clsDL.MvField(clsSF.FilePath, -1, "/")
                clsSF.Save()
            Else
                Dim clsSF As New clsSourceFiles
                clsSF.Read(strID)
                clsSF.TableId = Val(strTableID)
                clsSF.TableName = clsT.TableName
                clsSF.ProjectID = clsT.ProjectID
                clsSF.ProjectName = clsT.ProjectName
                clsSF.FilePath = strDE
                clsSF.ClassName = clsDL.MvField(clsSF.FilePath, -1, "/")
                clsSF.Save()
            End If
            ' open source code file
            WebSite.GotoPage("frmSourceCode.aspx?file=" & strDE)
        Else
            clsFineUI.MyAlert("Table Error", "Please select the table to build the business object for!")
        End If
    End Sub
End Class